package com.ruoyi.project.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wx.domain.CmsSlideDO;
import com.ruoyi.project.wx.domain.GoodsGoodsDO;
import com.ruoyi.project.wx.service.CmsSlideService;
import com.ruoyi.project.wx.service.GoodsGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 轮播图Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/wx/wxSlide")
public class CmsSlideController extends BaseController
{
	@Autowired
	private CmsSlideService cmsSlideService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;

	/**
	 * 查询轮播图列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxSlide:list')")
	@GetMapping("/list")
	public TableDataInfo list(CmsSlideDO cmsSlide)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cmsSlide);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","create_at");
		stringObjectMap.put("order","desc");
		List<CmsSlideDO> cmsSlideList = cmsSlideService.list(stringObjectMap);
		if (cmsSlideList!=null){
			for (CmsSlideDO slideDO:cmsSlideList){
				String goodsId = slideDO.getGoodsId();
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(goodsId);
				if (goodsGoodsDO!=null){
					slideDO.setGoodsId(goodsGoodsDO.getName());
				}

			}
		}
		return getDataTable(cmsSlideList);
	}

	/**
	 * 导出轮播图列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxSlide:export')")
	@Log(title = "轮播图", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(CmsSlideDO cmsSlide)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cmsSlide);
		List<CmsSlideDO> cmsSlideList = cmsSlideService.list(stringObjectMap);
		if (cmsSlideList!=null){
			for (CmsSlideDO slideDO:cmsSlideList){
				String goodsId = slideDO.getGoodsId();
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(goodsId);
				if (goodsGoodsDO!=null){
					slideDO.setGoodsId(goodsGoodsDO.getName());
				}

			}
		}
		ExcelUtil<CmsSlideDO> util = new ExcelUtil<CmsSlideDO>(CmsSlideDO.class);
		return util.exportExcel(cmsSlideList, "wxSlide");
	}

	/**
	 * 获取轮播图详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxSlide:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		String[] split = id.split("&");
		AjaxResult ajaxResult = AjaxResult.success();
		Map<String,Object> map = new HashMap<>();
		map.put("sort","gmt_create");
		map.put("order","desc");
		map.put("del_flag","0");
		map.put("disabled","0");
		if(split.length>=2){
			map.put("name",split[1]);
		}

		ajaxResult.put("goodsList",goodsGoodsService.list(map));
		ajaxResult.put("data",cmsSlideService.get(split[0]));

		return ajaxResult;
	}

	/**
	 * 新增轮播图
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxSlide:add')")
	@Log(title = "轮播图", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody CmsSlideDO cmsSlide)
	{
		cmsSlide.setId(IdUtil.simpleUUID());
		cmsSlide.setDelFlag(0);
		cmsSlide.setCreateAt(new Date());
		return toAjax(cmsSlideService.save(cmsSlide));
	}

	/**
	 * 修改轮播图
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxSlide:edit')")
	@Log(title = "轮播图", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody CmsSlideDO cmsSlide)
	{
		cmsSlide.setCreateAt(new Date());
		return toAjax(cmsSlideService.update(cmsSlide));
	}

	/**
	 * 删除轮播图
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxSlide:remove')")
	@Log(title = "轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				CmsSlideDO cmsSlide = cmsSlideService.get(string);
				cmsSlide.setDelFlag(1);
				cmsSlide.setCreateAt(new Date());
				cmsSlideService.update(cmsSlide);
			}
		}
		return toAjax(ids.length);
		//return toAjax(cmsSlideService.deleteCmsSlideByIds(ids));
	}
}
